/**
 * Created by Administrator on 2016/6/21.
 */

module.exports = function(app){

    
    var bodyParser = require('body-parser');
   
    app.use(bodyParser.urlencoded({extended:true}));
   
    var multer = require('multer');
 
    var cookieParser = require('cookie-parser');
    var fs = require('fs');
    app.use(cookieParser());


    app.post('/register',function(req,res){
       

        req.body.ip = req.ip;
        
        req.body.time = new Date();
     
        function send(code,message){
            res.status(200).json({code,message})
        }

     
        function saveFile(){
           
            var filename = `users/${req.body.petname}.txt`;

          
            fs.exists(filename,function(exists){
                if(exists){
                   
                    send('error','用户名已经存在');
                    
                }else{
                    
                    fs.appendFile(filename,JSON.stringify(req.body),function(err){
                        //让用户知道结果是成功还是失败
                        if(err){
                            send('error','系统错误');
                        }else{
                            send('success','注册成功');
                        }
                    })
                }
            })
        }
        //创建我们的users文件夹
        fs.exists('users',function(exists){
            if(exists){
                saveFile();
            }else{
              
                fs.mkdir('users',function(err){
                    if(err){
                        send('error','系统错误');
                    }else{
                        saveFile();
                    }
                })
            }
        })
    })
//登录的处理
    app.post('/signin',function(req,res){
       
        var filename = `users/${req.body.petname}.txt`;
        function send(code,message){
            res.status(200).json({code,message});
        }
      
        fs.exists(filename,function(exists){
            if(exists){
               
                fs.readFile(filename,function(err,data){
                    if(err){
                        send('error','系统错误');
                    }else{
                       
                        var user = JSON.parse(data);
                      
                        if(user.password == req.body.password){
                           
                            res.cookie('petname',req.body.petname);
                            send('success','登录成功');
                        }
                        else {
                            send('error','密码错误');
                        }
                    }
                })
            }else{
                send('error','用户名不存在');
            }
        })
    })
    

//登录
    app.get('/signin',function(req,res){
        res.render('signin',{
            user:req.cookies.petname,
            title:'登录页面'
        })
    })
//注册
    app.get('/register',function(req,res){
        res.render('register',{
            user:req.cookies.petname,
            title:'注册页面'
        })
    })
    app.get('/',function(req,res){
        res.render('index',{
            user:req.cookies.petname,
            title:'首页'
        })
    })
    app.get('/xiaomi5',function(req,res){
        res.render('xiaomi5',{
            user:req.cookies.petname,
            title:'首页'
        })
    })
    app.get('/signout',function(req,res){
        res.clearCookie('petname');
        res.redirect('/');
    })


}